package net.cyan.arachne.exts;

import net.cyan.arachne.*;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.Tag;

/**
 * @author camel
 * @date 2008-12-4
 */
public class FormJspTag implements Tag
{
    private PageContext pageContext;

    private String id;

    private String type;

    public FormJspTag()
    {
    }

    public void setPageContext(PageContext pageContext)
    {
        this.pageContext = pageContext;
    }

    public void setParent(Tag tag)
    {
    }

    public Tag getParent()
    {
        return null;
    }

    public int doStartTag() throws JspException
    {
        try
        {
            JspWriter writer = pageContext.getOut();

            if (pageContext.getAttribute("arachneImported") == null)
            {
                writer.write("<script src=\"");
                writer.write(PageConfig.getContextPath("/cyan/base.js"));
                writer.write("\"></script>\n");
                writer.write("<script src=\"");
                writer.write(PageConfig.getContextPath("/cyan/arachne.js"));
                writer.write("\"></script>\n");
                writer.write("<script src=\"");
                writer.write(PageConfig.getContextPath("/cyan/validate.js"));
                writer.write("\"></script>\n");
            }

            Class c = Class.forName(type);
            PageClass pageClass = PageClass.getClassInfo(c);


            writer.println("<script>");
            writer.println(pageClass.getObjectScript(id));
            writer.println("</script>");

            pageContext.setAttribute("arachneImported", true);

            return EVAL_BODY_INCLUDE;
        }
        catch (RuntimeException ex)
        {
            throw ex;
        }
        catch (Exception ex)
        {
            throw new JspException(ex);
        }
    }

    public int doEndTag() throws JspException
    {
        return EVAL_PAGE;
    }

    public void release()
    {
    }

    public String getId()
    {
        return id;
    }

    public void setId(String id)
    {
        this.id = id;
    }

    public String getType()
    {
        return type;
    }

    public void setType(String type)
    {
        this.type = type;
    }
}
